package top.someapp.user;

import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import top.someapp.R;
import top.someapp.annotation.BindView;
import top.someapp.base.BaseFragment;
import top.someapp.main.MainFragment;

/**
 * @author zwz
 * Created on 2021-07-22
 */
public class LoginFragment extends BaseFragment implements LoginContract.View {

    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;

    private LoginContract.Presenter presenter;

    public LoginFragment() {
        this.presenter = new LoginPresenter(this);
    }

    @Override protected int getFragmentLayoutId() {
        return R.layout.fragment_login;
    }

    @Override protected void addListeners() {
        btnLogin.setOnClickListener(
            v -> presenter.doLogin());
    }

    @Override protected void onReady() {
        presenter.start();
        etUsername.requestFocus();
    }

    @Override public String getName() {
        return etUsername.getText().toString();
    }

    @Override public void setName(String name) {
        etUsername.setText(name);
    }

    @Override public String getPassword() {
        return etPassword.getText().toString();
    }

    @Override public void setPassword(String password) {
        etPassword.setText(password);
    }

    @Override public void onLoginResult(boolean success, String message) {
        if (success) {
            //Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
            replaceSelfTo(new MainFragment());
        } else {
            Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
        }
    }
}
